using System.Collections.Generic;
using UnityEngine;

public class WinningsCaller : MonoBehaviour
{
	public GameObject displayWindow;

	public LeaderboardsLayer displayLayer;

	public GameObject prizesWindow;

	public PrizesPopUpLayer prizesLayer;

	public bool displayLeaderboard;

	public bool test;

	private LeaderboardsAdapter winnings;

	private void Start()
	{
		BehaviourSingletonInit<GGPrizesBackend>.instance.onCompFinished -= DisplayWinnings;
		BehaviourSingletonInit<GGPrizesBackend>.instance.onCompFinished += DisplayWinnings;
		LeaderboardsAdapter leaderboardsAdapter = BehaviourSingletonInit<GGPrizesBackend>.instance.GetPrizesIfCompFinished();
		if (test && leaderboardsAdapter == null)
		{
			leaderboardsAdapter = LeaderboardsAdapter.CreateTestSegmentedLeaderboard();
		}
		if (leaderboardsAdapter != null)
		{
			DisplayWinnings(leaderboardsAdapter);
			return;
		}
		GGDebug.Log("Check Prizes");
		BehaviourSingletonInit<GGPrizesBackend>.instance.CheckPrizes(GGPlayerSettings.instance.GetDisplayEloRatingInt(), GameConstants.Instance.CountryForFlag(GGPlayerSettings.instance.Model.flag).countryName.ToLower());
	}

	private void OnDestroy()
	{
		BehaviourSingletonInit<GGPrizesBackend>.instance.onCompFinished -= DisplayWinnings;
	}

	public void DisplayWinnings(LeaderboardsAdapter winnings)
	{
		bool flag = false;
		this.winnings = winnings;
		foreach (LeaderboardsAdapter.SingleLeaderboardAdapter leaderboard in winnings.leaderboards)
		{
			if (winnings.leaderboards.Count > 0 && leaderboard.player.leaderboardIndex >= 0)
			{
				flag = true;
			}
		}
		if (flag)
		{
			Dictionary<string, LeaderboardsAdapter.ClientPrize> dictionary = new Dictionary<string, LeaderboardsAdapter.ClientPrize>();
			foreach (LeaderboardsAdapter.SingleLeaderboardAdapter leaderboard2 in winnings.leaderboards)
			{
				if (leaderboard2.winnings != null)
				{
					dictionary.Add(leaderboard2.name, leaderboard2.winnings);
				}
			}
			if (dictionary.Count > 0)
			{
				prizesLayer.Init(dictionary, OnDisplayPrizesComplete);
				NavigationManager.instance.Push(prizesWindow);
			}
			else if (displayLeaderboard)
			{
				displayLayer.LeaderboardSet(winnings);
				NavigationManager.instance.Push(displayWindow);
			}
		}
		ApplyWinnings(winnings);
	}

	private void OnDisplayPrizesComplete()
	{
		if (displayLeaderboard)
		{
			displayLayer.LeaderboardSet(winnings);
			NavigationManager.instance.Push(displayWindow);
		}
	}

	private void ApplyWinnings(LeaderboardsAdapter winnings)
	{
		BehaviourSingletonInit<GGPrizesBackend>.instance.ApplyWinnings(winnings, GGPlayerSettings.instance.walletManager);
		BehaviourSingletonInit<GGPrizesBackend>.instance.CheckPrizes(GGPlayerSettings.instance.GetDisplayEloRatingInt(), GameConstants.Instance.CountryForFlag(GGPlayerSettings.instance.Model.flag).countryName.ToLower());
	}
}
